package com.bootdo.wx.controller;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.wx.domain.MemberExtractDO;
import com.bootdo.wx.service.MemberExtractService;
import com.bootdo.wx.service.OrderComService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 用户提现表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-06-15 15:07:28
 */
 
@Controller
@RequestMapping("/wx/memberExtract")
public class MemberExtractController {
	@Autowired
	private MemberExtractService memberExtractService;
	@Autowired
	private OrderComService orderComService;
	
	@GetMapping()
	@RequiresPermissions("wx:memberExtract:memberExtract")
	String MemberExtract(){
	    return "wx/memberExtract/memberExtract";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:memberExtract:memberExtract")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<Map<String,Object>> memberExtractList = memberExtractService.getList(query);
		int total = memberExtractService.getListCount(query);
		PageUtils pageUtils = new PageUtils(memberExtractList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:memberExtract:add")
	String add(){
	    return "wx/memberExtract/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:memberExtract:edit")
	String edit(@PathVariable("id") String id,Model model){
		MemberExtractDO memberExtract = memberExtractService.get(id);
		model.addAttribute("memberExtract", memberExtract);
	    return "wx/memberExtract/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:memberExtract:add")
	public R save( MemberExtractDO memberExtract){
		if(memberExtractService.save(memberExtract)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:memberExtract:edit")
	public R update( MemberExtractDO memberExtract){
		memberExtractService.update(memberExtract);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:memberExtract:remove")
	public R remove( String id){
		if(memberExtractService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:memberExtract:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		memberExtractService.batchRemove(ids);
		return R.ok();
	}

	/**
	 * 审核
	 */
	@ResponseBody
	@PostMapping("/shen")
	@RequiresPermissions("wx:memberExtract:edit")
	public R shen(MemberExtractDO memberExtractDO){
		return memberExtractService.shen(memberExtractDO);
	}
	/**
	 * 导出提现excel表格
	 */
	@RequestMapping( "/exportExcel")
	@RequiresPermissions("wx:memberExtract:memberExtract")
	public void exportExcel(@RequestParam Map<String,Object> map,
							HttpServletResponse response,HttpServletRequest request){
		try
		{
			//1.查找列表
			List<Map<String,Object>> orderList = memberExtractService.getList(map);
			//这里设置的文件格式是application/x-excel
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachment;filename=" + new String("提现列表.xls".getBytes(), "ISO-8859-1"));
			ServletOutputStream outputStream = response.getOutputStream();
			memberExtractService.exportExcel(orderList, outputStream,request);
			if(outputStream != null) {
				outputStream.close();
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	@GetMapping("/MemberExtractPT")
	@RequiresPermissions("wx:memberExtract:memberExtract")
	String MemberExtractPT(){
		return "wx/memberExtract/memberExtractPT";
	}

	/**
	 * 获取个人\平台 提现总金额
	 */
	@ResponseBody
	@PostMapping("/getNumByUserT")
	@RequiresPermissions("wx:memberExtract:memberExtract")
	public R getNumByUserT(@RequestParam Map<String, Object> params){
		BigDecimal allPrice = memberExtractService.getNumByUserT(params);
		return R.ok().put("data",allPrice);
	}


	/*@GetMapping("/extractInfo/{id}")
	@RequiresPermissions("wx:memberExtract:edit")
	String extractInfo(@PathVariable("id") String id,Model model){
		Map<String,Object> map = new HashMap<>();
		map.put("extractId",id);
		map.put("del_flag","0");
		List<OrderComDO> list = orderComService.list(map);
		MemberExtractDO memberExtract = memberExtractService.get(id);
		model.addAttribute("extractList", list);
		return "wx/memberExtract/edit";
	}*/

	/*
	 * 后台提现详情列表
	 * */
	@GetMapping("/extractInfo/{id}")
	@RequiresPermissions("wx:memberExtract:edit")
	String extractInfo(@PathVariable("id") String id,Model model){
		model.addAttribute("extractId", id);
		return "wx/memberExtract/extractInfo";
	}

	@ResponseBody
	@GetMapping("/getExtractInfoList")
	@RequiresPermissions("wx:memberExtract:edit")
	public PageUtils getMoneyList(@RequestParam Map<String, Object> params){
		//查询余额列表
		params.put("del_flag","0");
		Query query = new Query(params);
		List<Map<String,Object>> list = orderComService.getByExtractId(query);
		/*if(list.size()>0){

		}
		List<Map<String,Object>> list = memberUserService.getMoneyListByUser(query);
		if(list.size()>0){
			for (Map<String, Object> map : list) {
				if(null!=map.get("order_sn")&&!"".equals(map.get("order_sn"))){
					OrderOrderDO byOrder = orderOrderService.getByOrder(map.get("order_sn").toString());
					if(byOrder!=null){
						MemberUserDO memberUserDO = memberUserService.get(byOrder.getUserId());
						if(memberUserDO!=null){
							map.put("userName", memberUserDO.getName());
							map.put("level", memberUserDO.getLevel());
							MemberUserDO memberUserDO2 = memberUserService.get(memberUserDO.getIntroUser());
							if(memberUserDO2!=null){
								map.put("upName", memberUserDO2.getName());
							}else{
								map.put("upName", "");
							}

						}
					}


				}else{
					MemberUserDO memberUserDO = memberUserService.get(params.get("userId").toString());
					MemberUserDO memberUserDO2 = memberUserService.get(memberUserDO.getIntroUser());
					map.put("userName", memberUserDO.getName());
					map.put("level", memberUserDO.getLevel());
					map.put("upName", memberUserDO2.getName());
				}
			}
		}*/
		int total = orderComService.getByExtractIdCount(query);
		PageUtils pageUtils = new PageUtils(list, total);
		return pageUtils;
	}



	
}
